<?php

use think\facade\Db;
use think\migration\Migrator;
use think\migration\db\Column;

class AttachmentCate extends Migrator
{

    public function change()
    {
        $table = $this->table('attachment_cate', ['comment' => '文件分类', 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']);
        $table
            ->addColumn('name', 'string', ['limit' => 255, 'default' => '', 'comment' => '名称'])
            ->addColumn('parent_id', 'integer', ['limit' => 11, 'default' => '0', 'comment' => '父级id'])
            ->addColumn('level', 'integer', ['limit' => 1, 'default' => '1', 'comment' => '等级'])
            ->addColumn('sort', 'integer', ['limit' => 255, 'default' => '50', 'comment' => '排序'])
            ->addColumn('create_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '更新时间'])
            ->addColumn('delete_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '删除时间'])
            ->create();
    }

    protected function insertData()
    {
        $data = '[{"id":1,"name":"\u5168\u90e8","parent_id":0,"level":1,"sort":50,"create_time":1635934439,"update_time":1635934439,"delete_time":0}]';
        $msg = '文件分类导入成功.' . "\n";
        Db::startTrans();
        $data = json_decode($data, true);
        try {
            foreach ($data as $item) {
                \app\common\model\AttachmentCate::create($item);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $msg = $e->getMessage();
        }
        print ($msg);
    }
}
